@page "/pdf/digital-signature"
@inject Microsoft.JSInterop.IJSRuntime JS
@using System.IO
@using Microsoft.AspNetCore.Http;
@using Syncfusion.Blazor.Buttons
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment
@inherits SampleBaseComponent;
@*Hidden:Lines*@

@using BlazorDemos.Data.FileFormats.PDF

@*End:Hidden*@

<SampleDescription>
    <p>This sample demonstrates how a PDF document can be secured with certificates and signed with either standard or author signatures. Now added the support for Elliptic Curve Digital Signature Algorithm (ECDSA) to sign the PDF document.</p>
</SampleDescription>
<ActionDescription>
<p>It is also possible to digitally sign with <a target='_blank' href='https://help.syncfusion.com/file-formats/pdf/working-with-digitalsignature?&_ga=2.217597654.1703522802.1584005056-1302582622.1556169414#adding-a-timestamp-in-digital-signature'>
timestamp.</a> More information about the digital signature and timestamp can be found in this documentation <a target='_blank'
href='https://help.syncfusion.com/file-formats/pdf/working-with-digitalsignature?_ga=2.45302628.1703522802.1584005056-1302582622.1556169414'> section.</a></p>
</ActionDescription>

<div class="control-section">
    <div style="font-size:14px">
        <table>
            <tr>
                <td>
                    Click the button to view a PDF document generated by Essential PDF. Please note
                    that Adobe Reader or its equivalent is required to view the resultant document.
                </td>
            </tr>
        </table>
    </div>
    <div id="errorlabel" value="error">
    </div>
    <div class="tablediv" style="font-size:14px">
        <div class="rowdiv">
            <div class="celldiv">
                <table width="100%">
                    <tbody>
                        <tr>
                            <td align="left">
                                <span id="label8">Signature Settings</span>
                                <div style="border: solid 1px #788DB3; line-height: 23px; padding: 10px; ">
                                    <table cellspacing="0" cellpadding="0" border="0">
                                        <tbody>
                                            <tr>
                                                <td valign="middle" style="padding-left: 0px; width: 100%;">
                                                    <span id="label4">Cryptographic Standard: </span>
                                                    <span style="margin-left: 10px">
                                                        <SfRadioButton Label="CMS" Name="Cryptographic" ID="RadioButtonList2_0" Value=@("CMS") @bind-Checked="@Cryptographic"/>                                                      
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="CAdES" Name="Cryptographic" ID="RadioButtonList2_1" Value=@("CAdES") @bind-Checked="@Cryptographic"/>
                                                    </span>
                                                </td>

                                            </tr>
                                            <tr>
                                                <td valign="middle" style="padding-left: 0px; width: 100%;">
                                                    <br />
                                                    <span id="label4">Digest Algorithm: </span>
                                                    <span style="margin-left: 10px">
                                                        <SfRadioButton Label="SHA1" Name="Digest_Algorithm" ID="RadioButtonList3_0" Value=@("SHA1") @bind-Checked="@Digest_Algorithm"/>
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="SHA256" Name="Digest_Algorithm" ID="RadioButtonList3_1" Value=@("SHA256") @bind-Checked="@Digest_Algorithm" />
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="SHA384" Name="Digest_Algorithm" ID="RadioButtonList3_2" Value=@("SHA384") @bind-Checked="@Digest_Algorithm" />
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="SHA512" Name="Digest_Algorithm" ID="RadioButtonList3_3" Value=@("SHA512") @bind-Checked="@Digest_Algorithm" />
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="RIPEMD160" Name="Digest_Algorithm" ID="RadioButtonList3_4" Value=@("RIPEMD160") @bind-Checked="@Digest_Algorithm" />

                                                    </span>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <br />
        <div class="rowdiv">
            <div class="celldiv">
                <table width="100%">
                    <tbody>
                        <tr>
                            <td align="left">
                                <span id="label8">Sign new PDF document</span><br>
                                <div style="border: solid 1px #788DB3; line-height: 23px; padding: 10px; ">
                                    <table cellspacing="0" cellpadding="0" border="0">
                                        <tbody>
                                            <tr>
                                                <td valign="middle" style="padding-left: 0px; width: 100%;">
                                                    <span id="label4">Standard Type: </span>
                                                    <span style="margin-left: 10px">
                                                        <SfRadioButton Label="Standard" Name="RadioButtonList2" ID="RadioButtonList4_0" Value=@("Standard")  @bind-Checked="@RadioButtonList2" />
                                                    </span>
                                                    <span style="margin-left: 20px">
                                                        <SfRadioButton Label="Author" Name="RadioButtonList2" ID="RadioButtonList4_1" Value=@("Author") @bind-Checked="@RadioButtonList2" />
                                         
                                                    </span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="left">
                                                    <br />
                                                    <input name="submit" class="e-btn" id="submit" type="submit" @onclick="DigitalSignaturePDF" value="Create PDF" style="width: 150px;" />
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>

            </div>
        </div>
    </div>
</div>


<style>
    .radio-control {
        margin: 0 0 5% 0;
    }

    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {
   
   
    string  RadioButtonList2 = "Standard", NewPDF = null, Cryptographic = "CMS", Digest_Algorithm = "SHA256";
    /// <summary>
    /// Create and download the simple PDF document
    ///<summary>
    protected async void DigitalSignaturePDF(MouseEventArgs args)
    {        
        DigitalSignatureService service = new DigitalSignatureService(hostingEnvironment);
        MemoryStream documentStream = service.DigitalSignaturePDF(RadioButtonList2, NewPDF, Cryptographic, Digest_Algorithm);
        await JS.SaveAs("SignedPDF.pdf", documentStream.ToArray());
    }
}

